package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.LitemallActivityContentMapper;
import org.linlinjava.litemall.db.domain.LitemallActivityContent;
import org.linlinjava.litemall.db.domain.LitemallActivityContentExample;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class LitemallActivityContentService {

    @Resource
    private LitemallActivityContentMapper activityContentMapper;

    public List<LitemallActivityContent> queryActivityContent(Integer activityId, List<Byte> types) {
        if (activityId == null) {
            return Collections.emptyList();
        }
        LitemallActivityContentExample example = new LitemallActivityContentExample();
        LitemallActivityContentExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId);
        if (!CollectionUtils.isEmpty(types)) {
            criteria.andTypeIn(types);
        }
        criteria.andDeletedEqualTo(LitemallActivityContent.NOT_DELETED);
        return activityContentMapper.selectByExample(example);
    }

    public void addActivityContent(LitemallActivityContent entity, LitemallAdmin admin) {
        entity.setAddBy(admin.getId().toString());
        entity.setUpdateBy(admin.getId().toString());
        entity.setAddTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        entity.setDeleted(LitemallActivityContent.NOT_DELETED);
        activityContentMapper.insertSelective(entity);
    }

    public int update(LitemallActivityContent activityContent, LitemallAdmin admin) {
        activityContent.setUpdateTime(LocalDateTime.now());
        activityContent.setContent(activityContent.getContent());
        activityContent.setUpdateBy(admin.getId().toString());
        LitemallActivityContentExample example = new LitemallActivityContentExample();
        example.or().andActivityIdEqualTo(activityContent.getActivityId()).andTypeEqualTo(activityContent.getType());
        return activityContentMapper.updateByExampleSelective(activityContent, example);
    }
}
